package dxn.nl.file.api;

import com.alibaba.excel.util.StringUtils;
import dxn.boot.common.entity.*;
import dxn.boot.starter.web.control.BaseControl;
import dxn.boot.starter.web.security.AuthUtils;
import dxn.nl.file.biz.po.AppModelPo;
import dxn.nl.file.biz.vo.CsExportModelUserBindVo;
import dxn.nl.file.service.AppModeUserBindService;
import dxn.nl.file.service.CsExportAppModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created on 2020-10-23
 */
@Slf4j
@RestController
@RequestMapping("/mode/user")
@Tag(name = "模块绑定用户管理", description = "模块绑定用户管理")
public class AppModelUserControl extends BaseControl {

    @Resource
    private CsExportAppModelService csExportAppModelService;

    @Resource
    private AppModeUserBindService appModeUserBindService;

    @Operation(summary = "5 根据用户编号查询所有可以处理的导出模块")
    @RequestMapping(value = "/selectAllByUserCode", method = RequestMethod.POST)
    public BaseResult<List<AppModelPo>> selectAllByUserCode(@RequestBody @Valid CsExportModelUserBindVo vo) {

        AuthUser userDetails = AuthUtils.getCurrentUser();
        if(!"999999".equals(userDetails.getOrgCode()))
        {
            vo.setOrgCode(userDetails.getOrgCode());
        }

        List<AppModelPo> result = csExportAppModelService.selectAllByUserCode(vo.getOrgCode(),vo.getUserCode(),vo.getExportTypeCode());

        return BaseResult.success(result);
    }

    @Operation(summary = "绑定成员")
    @RequestMapping(value = "/userBindMode", method = RequestMethod.POST)
    public BaseResult<String> userBindMode(@RequestBody @Valid CsExportModelUserBindVo vo) {

        AuthUser userDetails = AuthUtils.getCurrentUser();
        if(!"999999".equals(userDetails.getOrgCode()))
        {
            vo.setOrgCode(userDetails.getOrgCode());
        }

        String result = appModeUserBindService.userBindMode(vo);

        return checkAndReturnResult(result);
    }

    @Operation(summary = "绑定成员")
    @RequestMapping(value = "/modeBindUser", method = RequestMethod.POST)
    public BaseResult<String> bindUser(@RequestBody @Valid CsExportModelUserBindVo vo) {

        AuthUser userDetails = AuthUtils.getCurrentUser();
        if(!"999999".equals(userDetails.getOrgCode()) || StringUtils.isBlank(vo.getOrgCode()))
        {
            vo.setOrgCode(userDetails.getOrgCode());
        }

        String result = appModeUserBindService.modeBindUser(vo);

        return checkAndReturnResult(result);
    }

    @Operation(summary = "解绑成员")
    @RequestMapping(value = "/modeUnBindUser", method = RequestMethod.POST)
    public BaseResult<String> modeUnBindUser(@RequestBody @Valid CsExportModelUserBindVo vo) {

        AuthUser userDetails = AuthUtils.getCurrentUser();
        if(!"999999".equals(userDetails.getOrgCode())|| StringUtils.isBlank(vo.getOrgCode()))
        {
            vo.setOrgCode(userDetails.getOrgCode());
        }

        String result = appModeUserBindService.modeUnBindUser(vo);

        return checkAndReturnResult(result);
    }

    @Operation(summary = "分页查询已绑定成员")
    @RequestMapping(value = "/pageModeBindUser", method = RequestMethod.POST)
    public BaseResult<CsBasePageResult<CsBaseCodeNameDto>> pageModeBindUser(@RequestBody @Valid CsBasePageQuery<CsExportModelUserBindVo> pageQuery) {

        AuthUser userDetails = AuthUtils.getCurrentUser();
        if(!"999999".equals(userDetails.getOrgCode()) || StringUtils.isBlank(pageQuery.getQuery().getOrgCode()))
        {
            pageQuery.getQuery().setOrgCode(userDetails.getOrgCode());
        }

        CsBasePageResult<CsBaseCodeNameDto> result = appModeUserBindService.pageModeBindUser(pageQuery);

        return BaseResult.success(result);
    }

    @Operation(summary = "分页查询未绑定成员")
    @RequestMapping(value = "/pageModeUnBindUser", method = RequestMethod.POST)
    public BaseResult<CsBasePageResult<CsBaseCodeNameDto>> pageModeUnBindUser(@RequestBody @Valid CsBasePageQuery<CsExportModelUserBindVo> pageQuery) {

        AuthUser userDetails = AuthUtils.getCurrentUser();
        if(!"999999".equals(userDetails.getOrgCode()) || StringUtils.isBlank(pageQuery.getQuery().getOrgCode()))
        {
            pageQuery.getQuery().setOrgCode(userDetails.getOrgCode());
        }

        CsBasePageResult<CsBaseCodeNameDto> result = appModeUserBindService.pageModeUnBindUser(pageQuery);

        return BaseResult.success(result);
    }
}
